package eproject.mm.com.projectsupervision.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import eproject.mm.com.projectsupervision.PrjApplication;
import eproject.mm.com.projectsupervision.R;
import eproject.mm.com.projectsupervision.widget.IconFontTextview;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class PasswordActivity extends Activity {
    IconFontTextview iftv_back;
    TextView et_oldPassword,et_newPassword,et_rePassword;
    private OkHttpClient client;
    LoadingDialog loadingDialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);

        et_oldPassword = (TextView)findViewById(R.id.et_oldPassword);
        et_newPassword = (TextView)findViewById(R.id.et_newPassword);
        et_rePassword = (TextView)findViewById(R.id.et_rePassword);

        iftv_back = (IconFontTextview)findViewById(R.id.iftv_back);
        iftv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        client = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .build();
    }
    Handler handler = new Handler();

    public void onSubmit(View view){
        final String oldPass = et_oldPassword.getText().toString().trim();
        final String newPass = et_newPassword.getText().toString().trim();
        String rePass = et_rePassword.getText().toString().trim();

        if(newPass.equals("")){
            Toast.makeText(this,"新密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }else if(newPass.length()<6){
            Toast.makeText(this,"新密码不能少于6位",Toast.LENGTH_SHORT).show();
            return;
        }else if(!newPass.equals(rePass)){
            Toast.makeText(this,"新密码两次输入不一致",Toast.LENGTH_SHORT).show();
            return;
        }

        loadingDialog = new LoadingDialog(this);
        loadingDialog.setLoadingText("加载中...").show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    okhttp3.FormBody.Builder builder = new okhttp3.FormBody.Builder();
                    builder.add("oldpassword",oldPass);
                    builder.add("newpassword",newPass);

                    RequestBody requestBody = builder.build();
                    Request.Builder okbuilder = new Request.Builder();
                    okbuilder.addHeader("authenticationToken", PrjApplication.getInstance().getAuthenticationToken());
                    Request requestPostCard = okbuilder
                            .url("http://"+ PrjApplication.getInstance().getServerIp() +":"+PrjApplication.getInstance().getServerPort()+"/"+PrjApplication.getInstance().getPreFixPath()+"/passwordModify")
                            .post(requestBody)
                            .build();
                    client.newCall(requestPostCard).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            loadingDialog.close();
                        }

                        @Override
                        public void onResponse(Call call, final Response response) throws IOException {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    loadingDialog.close();
                                    String str = "{}";
                                    try{
                                        ResponseBody body = response.body();
                                        if(body!=null) {
                                            str = body.string();
                                        }
                                    }catch (Exception ioe){

                                    }
                                    Gson gson=new Gson();
                                    Map resultMap = gson.fromJson(str, Map.class);
                                    if (resultMap != null && (boolean) resultMap.get("success")) {
                                        Toast.makeText(PasswordActivity.this,"密码修改成功",Toast.LENGTH_LONG).show();
                                    }else{
                                        String message = (String)resultMap.get("message");
                                        Toast.makeText(PasswordActivity.this,message,Toast.LENGTH_LONG).show();
                                    }
                                }
                            });
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(PasswordActivity.this,e.toString(),Toast.LENGTH_LONG);
                }
            }
        }).start();
    }
    public void back(View view) {
        finish();
    }
}
